package com.itheima.book.manager.dao;

import com.itheima.book.manager.domain.Book;

public class BookDao implements BaseBookDao {
    //?为了让我们BookDao中的所有方法都可以进行访问，我们就需要将这个数组定义成成员变量
//    private Book[] books = new Book[5];
    private static Book[] books = new Book[5];
    public boolean addBook(Book book){
//        Book[] books = new Book[5];
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] == null){
                index = i;
                break;
            }
        }
        if (index != -1){
            books[index] = book;
            return true;
        }
        return false;
    }

    public Book[] getAllBooks(){
        return books;
    }
    /*
    public void deleteByBookId(String bookId){
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null){
                if (books[i].getBookID().equals(bookId)){
                    books[i] = null;
                }
            }
        }
    }
    */
    public void deleteByBookId(String bookId){
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null){
                if (books[i].getBookID().equals(bookId)){
                    index = i;
                }
            }
        }
        if (index != -1){
            books[index] = null;
        }
    }

    public void updateBookByBookId(Book book){
//        int index = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null){
                if (books[i].getBookID().equals(book.getBookID())){
                    books[i] = book;
//                    System.out.println("修改图书成功");
//                    index = i;
//                    return true;
                }
            }
        }
//        if (index != -1){
//            books[index] = book;
//        }
//        return false;
    }
}
